<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item class="dislabel">
        <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click.native="handlerOpenInfoAdd(0)" class="position-btn">新增</el-button>
      </el-form-item>
      <el-table :data="pram.checkList" size="mini" :header-cell-style=" {fontWeight:'bold'}" border>
        <!--        <template slot="empty"><span>暂无数据</span></template>-->
        <el-table-column label="年度" prop="year" min-width="100" align="center"/>
        <el-table-column label="考评等次" prop="checkResultText" min-width="100" align="center"/>
        <el-table-column label="操作" width="100"  fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="handlerInfoDel(scope.$index,scope.row)" v-hasPermi="['admin:person:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-form-item class="btn-box">
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:update','admin:person:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>

    <!-- 职务详情 -->
    <el-dialog
      :visible.sync="checkInfoDialogConfig.visible"
      :title="checkInfoDialogConfig.isCreate === 0? '新增考评等次':'新增考评等次'"
      destroy-on-close
      :close-on-click-modal="false"
      :append-to-body="true"
      width="700px"
    >
      <info
        v-if="checkInfoDialogConfig.visible"
        :is-create="checkInfoDialogConfig.isCreate"
        :edit-data="checkInfoDialogConfig.editData"
        :edit-key="checkInfoDialogConfig.editKey"
        :all-check-data="checkData"
        @hideInfoDialog="hideInfoDialog"
        @assignInfoSaveAndHideInfoDialog="assignInfoSaveAndHideInfoDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
import info from './info'
export default {
  // name: "edit"
  components: { info },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allCheckData: {
      type: Array
    }
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        checkList: [],

        checkDesc:'',
        //编辑额外
        removeId:'',
        addArr:[],
      },
      removeTempId:'',

      rules: {
        //positionDesc: [{ required: true, message: '请填简称', trigger: ['blur', 'change'] }],
      },
      checkInfoDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {},
        editKey: -1,
      },
      checkData:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideCheckDialog')
    },

    initEditData() {
      this.checkData = this.allCheckData

      //if (this.isCreate !== 1) return //编辑
      const { checkList} = JSON.parse(JSON.stringify(this.editData)) //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.checkList = checkList||[]
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      const checkList = this.pram.checkList
      let checkDesc = ''
      checkList.forEach((item)=>{
        checkDesc += item.year+"年"+item.checkResultText+"、"
      })
      this.pram.checkDesc = checkDesc.substring(0,checkDesc.length -1)
      this.$emit('assignCheckSaveAndHideCheckDialog',this.pram)
    },
    handlerEdit() {
      const checkList = this.pram.checkList
      let checkDesc = ''
      checkList.forEach((item)=>{
        checkDesc += item.year+"年"+item.checkResultText+"、"
      })
      this.pram.checkDesc = checkDesc.substring(0,checkDesc.length -1)

      this.pram.addArr=this.pram.checkList.filter(item=>!item.id)
      this.pram.removeId = this.removeTempId
      this.$emit('assignCheckEditAndHideCheckDialog',this.pram)
    },

    // 考核新增
    handlerOpenInfoAdd(isCreate, editDate, editKey) {
      this.checkInfoDialogConfig.editData = editDate
      this.checkInfoDialogConfig.isCreate = isCreate
      this.checkInfoDialogConfig.visible = true
      this.checkInfoDialogConfig.editKey = editKey
    },
    hideInfoDialog() {
      this.checkInfoDialogConfig.visible = false
    },
    assignInfoSaveAndHideInfoDialog(data) {
      setTimeout(() => {
        this.pram.checkList.push(data);
        this.checkInfoDialogConfig.visible = false
      }, 200);
    },
    handlerInfoDel(key,row){
      //删除单行考核数据
      const  _xKey = key
      this.pram.checkList.splice(_xKey,1);//index 当前元素索引；1：需要删除的元素个数
      //data=data.filter(item=>item.name!='张三');

      if (this.isCreate === 1 && row.id) {
        //编辑时的删除，记录删除ID
        const delId = row.id
        this.removeTempId+=delId+','
      }
    },

  }
}
</script>

<style scoped lang="scss">
  .position-btn{
    float:right;
  }
  .btn-box{
    margin-top:20px;
  }
  .dislabel{
    ::v-deep.el-form-item__label{
      width:50px!important;
    }
    ::v-deep.el-form-item__content{
      margin-left:50px!important;
    }
    ::v-deep.el-input--small .el-input__inner,
    ::v-deep.el-input--medium .el-input__inner{
      height:36px;
      line-height:36px;
      font-size:12px;
    }
    ::v-deep.el-textarea__inner{
      font-size:12px;
    }

  }
</style>
